﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace ADRoot
{
    class ConfigurationManagement
    {
        /// <summary>
        /// 通过Key找到value
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string getConfigByKey(string key)
        {
            string val = string.Empty;
            if (ConfigurationManager.AppSettings.AllKeys.Contains(key))
            {
                val = ConfigurationManager.AppSettings[key];
            }
            return val;
        }
        /// <summary>
        /// 获得所有的项及其值
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> GetAllConfig()
        {
            Dictionary<string, string> dict = new Dictionary<string, string>();
            foreach (string key in ConfigurationManager.AppSettings.AllKeys)
            {
                dict.Add(key, ConfigurationManager.AppSettings[key]);
            }
            return dict;
        }

        /// <summary>
        /// 根据Key取得值，如果没有值，则返回默认值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="defaultKey"></param>
        /// <returns></returns>
        public static string getConfigByKey(string key, string defaultKey)
        {
            string val = defaultKey;
            if (ConfigurationManager.AppSettings.AllKeys.Contains(key))
            {
                val = ConfigurationManager.AppSettings[key];
            }
            if (val == null)
            {
                val = defaultKey;
            }
            return val;
        }
        /// <summary>
        /// 写配置文件，如果不存在，则创建，如果存在，则更改。正确操作返回真，否则返回假
        /// </summary>
        /// <param name="key"></param>
        /// <param name="val"></param>
        /// <returns></returns>
        public static bool setConfig(string key, string val)
        {
            try
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                if (!ConfigurationManager.AppSettings.AllKeys.Contains(key))
                {
                    config.AppSettings.Settings.Add(key, val);
                }
                else
                {
                    config.AppSettings.Settings[key].Value = val;
                }
                config.Save();
                return true;

            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool setConfig(Dictionary<string, string> dict)
        {

            try
            {
                if (dict.Count == 0 || dict == null)
                {
                    return false;
                }
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                foreach (string key in dict.Keys)
                {
                    if (!config.AppSettings.Settings.AllKeys.Contains(key))
                    {
                        config.AppSettings.Settings.Add(key, dict[key]);
                    }
                    else
                    {
                        config.AppSettings.Settings[key].Value = dict[key];
                    }
                }
                config.Save();
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }
    }
}